/*!
* Energy Savings Tips Calculator
* Copyright (c) 2009 FortisAlberta
* Author: Bil Simser (bil.simser@fortisalberta.com)
* Modified by: Graham Robinson (graham.robinson@fortisbc.com)
*/

// price per kwh - Jan 2011
var rate = '0.09090' 

$(document).ready(function() {

    // bind the recalc function to the quantity fields
    $("input[name^=qty_item_]").bind("keyup", recalc);
    $("input[name^=wat_item_]").bind("keyup", recalc);
    $("input[name^=price_item_]").bind("keyup", recalc);

    // reset the form
    reset();

    // run the calculation function now
    recalc();

    // setup the calculator formatting
    $(".calculator-head").click(function() {
        $(this).toggleClass('expanded').next().toggle('fast');
        return false;
    }).next().hide();

});

function reloadForm() {
    reset();
    recalc();
}

function reset() {
    $("input[name^=qty_item_]").val('0');
}

function recalc() {

    // calculate consumption updates
    $("[id^=consumption_item]").calc(
        // the equation to use for the calculation
        "if(price != 0) { Math.round((qty * wattage * price) / 1000) } else { qty * wattage }",
        // define the variables used in the equation, these can be a jQuery object
        {
            qty: $("input[name^=qty_item_]"),
            wattage: $("input[name^=wat_item_]"),
            price: $("[name^=price_item_]")
        },
        // define the formatting callback, the results of the calculation are passed to this function
        function(s) {
            return s;
        },
        // define the finish callback, this runs after the calculation has been complete
        function($this) {
            var sum = $this.sum();
            $("#totalKwh").text(sum);
        }
    );

    // calculate monthly cost
    $("[id^=total_item]").calc(
        // the equation to use for the calculation
        "if(price != 0) { Math.round((qty * wattage * price) / 1000) * rate } else { (qty * wattage) * rate }",
        // define the variables used in the equation, these can be a jQuery object
        {
            qty: $("input[name^=qty_item_]"),
            wattage: $("input[name^=wat_item_]"),
            price: $("[name^=price_item_]")
        },
        // define the formatting callback, the results of the calculation are passed to this function
        function(s) {
            // return the number as a dollar 
            return "$" + s.toFixed(2);
        },
        // define the finish callback, this runs after the calculation has been complete
        function($this) {

            // sum the total of the $("[id^=total_item]") selector
            var sum = $this.sum();

            $("#grandTotal").text(
            // round the results to 2 digits
            "$" + sum.toFixed(2));
        });

        // calculate room totals
        for (var i = 1; i < 10; i++) {
            var subtotal = $("[id^=total_item_" + i + "]").sum();
            $("#subtotal_" + i).text("$" + subtotal.toFixed(2));
        }
}